<?php

namespace common\models\c2\search;

use cza\base\models\statics\EntityModelStatus;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\c2\entity\FeUser;

/**
 * FeUserSearch represents the model behind the search form about `common\models\c2\entity\FeUser`.
 */
class FeUserSearch extends FeUser
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'muid', 'proprietor_id', 'community_id', 'building_number_id', 'unit_id', 'house_id', 'type'], 'integer'],
            [['password_hash', 'username', 'mobile_number', 'id_number', 'email', 'address', 'status', 'created_at', 'updated_at'], 'safe'],
            [['balance'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = FeUser::find();

        $query->orWhere(['status' => FeUser::STATUS_UN_AUDIT]);
        $query->orWhere(['status' => EntityModelStatus::STATUS_INACTIVE]);

        if (!Yii::$app->user->can('P_SYSTEM')) {
            $this->community_id = Yii::$app->user->getCommunityId();
        }

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'sortParam' => $this->getSortParamName(),
            ],
            'pagination' => [
                'pageParam' => $this->getPageParamName(),
                'pageSize' => 20,
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'muid' => $this->muid,
            'proprietor_id' => $this->proprietor_id,
            'community_id' => $this->community_id,
            'building_number_id' => $this->building_number_id,
            'unit_id' => $this->unit_id,
            'house_id' => $this->house_id,
            'type' => $this->type,
            'balance' => $this->balance,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'password_hash', $this->password_hash])
            ->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'mobile_number', $this->mobile_number])
            ->andFilterWhere(['like', 'id_number', $this->id_number])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }
    
    public function getPageParamName($splitor = '-'){
        $name = "FeUserPage";
        return \Yii::$app->czaHelper->naming->toSplit($name);
    }
    
    public function getSortParamName($splitor = '-'){
        $name = "FeUserSort";
        return \Yii::$app->czaHelper->naming->toSplit($name);
    }
}
